﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 8/25/2009
 * Time: 9:25 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Miming.Model;
using Miming.Util;

namespace Miming.Gui
{
	/// <summary>
	/// Description of CustomerForm.
	/// </summary>
	public partial class CustomerForm : BaseForm
	{
		public CustomerForm() : this(new Customer())
		{
			
		}
		
		public CustomerForm(Customer c)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			Model = c;
		}
		
		protected override void OnModelChanged(EventArgs e)
		{
			base.OnModelChanged(e);
			Customer c = Model as Customer;
			textBoxName.Text = c.Name;
			textBoxOpeningBalance.Text = c.OpeningBalance.ToString("0.00");
			dateTimePickerBalanceAsOf.Value = c.BalanceAsOf;
			textBoxCompanyName.Text = c.CompanyName;
			textBoxTitle.Text = c.LegalName.Title;
			textBoxLastName.Text = c.LegalName.Last;
			textBoxFirstName.Text = c.LegalName.First;
			textBoxMI.Text = c.LegalName.Middle;
			richTextBoxBillTo.Text = c.BillToAddress;
			textBoxContact.Text = c.Contact;
			textBoxPhone.Text = c.Phone;
			textBoxFax.Text = c.Fax;
			textBoxAltPhone.Text = c.AltPhone;
			textBoxAltContact.Text = c.AltContact;
			textBoxEmail.Text = c.Email;
			textBoxCC.Text = c.CC;
			checkBoxInactive.Checked = c.Inactive;
		}
		
		void ButtonOkClick(object sender, EventArgs e)
		{
			Customer c = Model as Customer;
			c.Name = textBoxName.Text;
			c.OpeningBalance = ConvertUtility.ToDouble(textBoxOpeningBalance.Text);
			c.BalanceAsOf = dateTimePickerBalanceAsOf.Value;
			c.CompanyName = textBoxCompanyName.Text;
			c.LegalName = new Name(textBoxTitle.Text, textBoxLastName.Text, textBoxFirstName.Text, textBoxMI.Text);
			c.BillToAddress = richTextBoxBillTo.Text;
			c.Contact = textBoxContact.Text;
			c.Phone = textBoxPhone.Text;
			c.Fax = textBoxFax.Text;
			c.AltPhone = textBoxAltPhone.Text;
			c.AltContact = textBoxAltContact.Text;
			c.Email = textBoxEmail.Text;
			c.CC = textBoxCC.Text;
			c.Inactive = checkBoxInactive.Checked;
			SessionFacade.SaveOrUpdateCustomer(c);
			Close();
		}
		
		void ButtonCancelClick(object sender, EventArgs e)
		{
			Close();
		}
	}
}
